/**
  ******************************************************************************
  * @file       main.c
  * @author     embedfire
  * @version     V1.0
  * @date        2024
  * @brief      多串口通讯之人体雷达实验
  ******************************************************************************
  * @attention
  *
  * 实验平台  ：野火 STM32F103C8T6-STM32开发板 
  * 论坛      ：http://www.firebbs.cn
  * 官网      ：https://embedfire.com/
  * 淘宝      ：https://yehuosm.tmall.com/
  *
  ******************************************************************************
  */

#include "main.h"
#include "stm32f10x.h"
#include "usart/bsp_usart.h"
#include "led/bsp_gpio_led_ex.h"  
#include "delay/bsp_delay.h"
#include "rd03/bsp_rd03.h"

/**
  * @brief  主函数
  * @param  无 
  * @note   无
  * @retval 无
  */
int main(void)
{
    NVIC_PriorityGroupConfig(NVIC_PriorityGroup_4);
    LED_EX_GPIO_Config();
    Usart_Task_Init();
#if 1
    
    /* 开启命令模式 */
    {
        uint8_t cmd[]={0xfd,0xfc,0xfb,0xfa,0x04,0x00,0xff,0x00,0x01,0x00,0x04,0x03,0x02,0x01};
        USARTX_SendString_Lenth(RD03_USARTX,cmd,sizeof(cmd));
        Rough_Delay_Ms(150); //清空串口缓存数据（ 一般delay 100ms左右，确保串口数据都被清空） 
        Rd03_buff_Reset(&rd03_receive_data);
        USARTX_SendString_Lenth(RD03_USARTX,cmd,sizeof(cmd));
        //进行ack的解析
        Rd03_buff_Reset(&rd03_receive_data);
    } 
    /* 最小探测距离门 */ 
    {
        uint8_t write_cmd[]={0xfd,0xfc,0xfb,0xfa,0x08,0x00,0x07,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x04,0x03,0x02,0x01};
        USARTX_SendString_Lenth(RD03_USARTX,write_cmd,sizeof(write_cmd));
    }
    
    /* 最远探测距离门 */
    {
        uint8_t write_cmd[]={0xfd,0xfc,0xfb,0xfa,0x08,0x00,0x07,0x00,0x01,0x00,0x00,0x00,0x00,0x00,0x04,0x03,0x02,0x01};
        USARTX_SendString_Lenth(RD03_USARTX,write_cmd,sizeof(write_cmd));
    }
    
    /* 目标消失延迟时间 */
    {
        uint8_t write_cmd[]={0xfd,0xfc,0xfb,0xfa,0x08,0x00,0x07,0x00,0x04,0x00,0x00,0x00,0x00,0x00,0x04,0x03,0x02,0x01};
        USARTX_SendString_Lenth(RD03_USARTX,write_cmd,sizeof(write_cmd));
    }
    
    /* 关闭命令模式 */
    {
        uint8_t cmd[]={0xfd,0xfc,0xfb,0xfa,0x02,0x00,0xfe,0x00,0x04,0x03,0x02,0x01,0x00};
        Rd03_buff_Reset(&rd03_receive_data);
        USARTX_SendString_Lenth(RD03_USARTX,cmd,sizeof(cmd));
        //进行ack的解析
        Rd03_buff_Reset(&rd03_receive_data);
    } 
   
#endif

#if 1
    #define CURRENT_MODE RD03_REPORTING_MODE
    /* 开启命令模式 */
    {
        uint8_t cmd[]={0xfd,0xfc,0xfb,0xfa,0x04,0x00,0xff,0x00,0x01,0x00,0x04,0x03,0x02,0x01};
        USARTX_SendString_Lenth(RD03_USARTX,cmd,sizeof(cmd));
        Rough_Delay_Ms(150); //清空串口缓存数据（ 一般delay 100ms左右，确保串口数据都被清空） 
        Rd03_buff_Reset(&rd03_receive_data);
        USARTX_SendString_Lenth(RD03_USARTX,cmd,sizeof(cmd));
        //进行ack的解析
        Rd03_buff_Reset(&rd03_receive_data);
    } 
    switch(CURRENT_MODE)
    {
        case RD03_DEBUGGING_MODE:
            {
                uint8_t cmd[]={0xfd,0xfc,0xfb,0xfa,0x08,0x00,0x12,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x04,0x03,0x02,0x01};
                USARTX_SendString_Lenth(RD03_USARTX,cmd,sizeof(cmd));
            }
            break;
        case RD03_REPORTING_MODE:
            {
                uint8_t cmd[]={0xfd,0xfc,0xfb,0xfa,0x08,0x00,0x12,0x00,0x00,0x00,0x04,0x00,0x00,0x00,0x04,0x03,0x02,0x01};
                USARTX_SendString_Lenth(RD03_USARTX,cmd,sizeof(cmd));
            }
            break;
        case RD03_RUNNING_MODE:
            {
                uint8_t cmd[]={0xfd,0xfc,0xfb,0xfa,0x08,0x00,0x12,0x00,0x00,0x00,0x64,0x00,0x00,0x00,0x04,0x03,0x02,0x01};
                USARTX_SendString_Lenth(RD03_USARTX,cmd,sizeof(cmd));
            }
            break;
    }

    /* 关闭命令模式 */
    {
        uint8_t close_cmd[]={0xfd,0xfc,0xfb,0xfa,0x02,0x00,0xfe,0x00,0x04,0x03,0x02,0x01,0x00};
        Rd03_buff_Reset(&rd03_receive_data);
        USARTX_SendString_Lenth(RD03_USARTX,close_cmd,sizeof(close_cmd));
        //进行ack的解析
        Rd03_buff_Reset(&rd03_receive_data);
    }     
    
#endif    
    while(1)
    {
        Debug_Task(&debug_receive_data);
        Rd03_Task(&rd03_receive_data);

    }
        
}

